package com.guilin.controller;

import com.guilin.common.core.controller.BaseController;
import com.guilin.common.core.domain.AjaxResult;
import com.guilin.common.core.page.TableDataInfo;
import com.guilin.common.utils.poi.ExcelUtil;
import com.guilin.domain.NewsCategory;
import com.guilin.system.service.NewsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 新闻分类Controller
 *
 * @author xiaoqi
 * @date 2025-06-18
 */
@RestController
@RequestMapping("/system/category")
public class NewsCategoryController extends BaseController
{
    @Autowired
    private NewsCategoryService newsCategoryService;

    /**
     * 查询新闻分类列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(NewsCategory newsCategory)
    {
        startPage();
        List<NewsCategory> list = newsCategoryService.selectNewsCategoryList(newsCategory);
        return getDataTable(list);
    }

    /**
     * 导出新闻分类列表
     */
    @PreAuthorize("@ss.hasPermi('admin-management:category:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, NewsCategory newsCategory)
    {
        List<NewsCategory> list = newsCategoryService.selectNewsCategoryList(newsCategory);
        ExcelUtil<NewsCategory> util = new ExcelUtil<NewsCategory>(NewsCategory.class);
        util.exportExcel(response, list, "新闻分类数据");
    }

    /**
     * 获取新闻分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin-management:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(newsCategoryService.selectNewsCategoryById(id));
    }

    /**
     * 新增新闻分类
     */
    @PreAuthorize("@ss.hasPermi('admin-management:category:add')")
    @PostMapping
    public AjaxResult add(@RequestBody NewsCategory newsCategory)
    {
        return toAjax(newsCategoryService.insertNewsCategory(newsCategory));
    }

    /**
     * 修改新闻分类
     */
    @PreAuthorize("@ss.hasPermi('admin-management:category:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody NewsCategory newsCategory)
    {
        return toAjax(newsCategoryService.updateNewsCategory(newsCategory));
    }

    /**
     * 删除新闻分类
     */
    @PreAuthorize("@ss.hasPermi('admin-management:category:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(newsCategoryService.deleteNewsCategoryByIds(ids));
    }
}
